package com.mcxx.modules.minimumliving.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.common.exception.BizException;
import com.mcxx.common.service.impl.FundPayBizVerifyUtil;
import com.mcxx.constant.MQConstant;
import com.mcxx.modules.minimumliving.service.FiscalAbutmentService;
import com.mcxx.modules.minimumliving.service.FundPayService;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.base.service.read.BankMappingReadService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.dto.BankAbutmentDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.*;

import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayFamilyEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayMemberEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundpayExceptionEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayFamilyParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayMemberParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.*;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundPayFamilyWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundPayMemberWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundPayWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundpayExceptionWriteService;
import com.mcxx.modules.system.dto.MQMessageDTO;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.util.Constant;
import com.mcxx.util.HttpServiceUtil;
import com.mcxx.util.OkHttpUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
@TaTransactional
public class FundPayServiceImpl extends BaseService implements FundPayService {
    private static final Logger logger = LoggerFactory.getLogger(FundPayServiceImpl.class);
    @Autowired
    private FundPayReadService fundPayReadService;
    @Autowired
    private FundPayWriteService fundPayWriteService;
    @Autowired
    private FundPayFamilyWriteService fundPayFamilyWriteService;
    @Autowired
    private FundPayFamilyReadService fundPayFamilyReadService;
    @Autowired
    private FundPayMemberWriteService fundPayMemberWriteService;
    @Autowired
    private FundPayMemberReadService fundPayMemberReadService;
    @Autowired
    private CustomFundPayMemberReadService customFundPayMemberReadService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;
    @Autowired
    private AreaReadService areaReadService;
    @Autowired
    private BankMappingReadService bankMappingReadService;
    @Autowired
    private FiscalAbutmentService fiscalAbutmentService;
    @Autowired
    private FundpayExceptionReadService fundpayExceptionReadService;
    @Autowired
    private FundpayExceptionWriteService fundpayExceptionWriteService;
    @Autowired
    private SingleFundPayReadService singleFundPayReadService;
    @Value("${society.fundpay.url:http://10.22.197.53:8011/mc-fundpay/base-info/updateSubsidyObjectAndBankAccountByMqyByTwo}")
    private String fundpayUrl;
    @Value("${society.fundpay.delUrl:http://10.22.197.53:8011/mc-fundpay/fund-pay-info/deleteFundPayByFundPayId}")
    private String fundpayDelUrl;




    @Override
    public BaseResult<Map<String, List<String>>> delFundPay(List<String> fundPayIdList) {
        BaseResult<Map<String, List<String>>> result = new BaseResult<>();

        List<String> stateList = new ArrayList<String>(){{add(Constant.FundPayState.CREATE); add(Constant.FundPayState.REVOKE); add(Constant.FundPayState.CREATE_FAIL);}};
        Map<String, List<String>> rstMap = fundPayReadService.getIdByIdAndStates(fundPayIdList, stateList);
        if(rstMap.containsKey("success") == false){

            return BaseResult.fail("名册还未确认");
        }

        for(String fundpayId : fundPayIdList) {
            String retMsg = HttpServiceUtil.httpCallDelFundpayData(fundpayDelUrl, fundpayId);
            if(StringUtils.isNotEmpty(retMsg)) {
                return BaseResult.fail("同步删除发放系统名册失败:" + retMsg);
            }
        }


        int rst = fundPayWriteService.delFundPay(rstMap.get("success"));


        if(rst <= 0){

            rstMap.put("fail", fundPayIdList);
            rstMap.remove("success");
        }
        else {
            int count = fundpayExceptionWriteService.deleteByFundpayId(fundPayIdList);
            logger.info("删除名册相关的异常数据：{}", count);
        }

        result.setData(rstMap);
        return result;
    }


    @Override
    public BaseResult<Map<String, List<String>>> cancelFundPay(List<String> fundPayIdList) {
        BaseResult<Map<String, List<String>>> result = new BaseResult<>();

        Map<String, List<String>> rstMap = fundPayReadService.getIdByIdAndState(fundPayIdList, Constant.FundPayState.CONFIRM);
        if(rstMap.containsKey("success") == false){

            return BaseResult.fail("该名册还未确认");
        }

        List<String> row = singleFundPayReadService.verifyFundPayRollIsExist(fundPayIdList, null);//Constant.FundPayState.CONFIRM
        if (row.size() > 0) {
            return BaseResult.fail("该名册已经生成对应的一卡通发放名册，请先撤销 并且删除 对应的发放名册");
        }
        for(String fundpayId : fundPayIdList) {
            String retMsg = HttpServiceUtil.httpCallDelFundpayData(fundpayDelUrl, fundpayId);
            if(StringUtils.isNotEmpty(retMsg)) {
                return BaseResult.fail("同步删除发放系统名册失败:" + retMsg);
            }
        }


        int count = fundpayExceptionWriteService.deleteByFundpayId(fundPayIdList);
        logger.info("删除该名册下的所有异常信息，fundpayIds:{}, num:{}", fundPayIdList, count);


        int rst = fundPayWriteService.updateStateByIds(rstMap.get("success"), Constant.FundPayState.CREATE, null);


        if(rst <= 0){

            rstMap.put("fail", fundPayIdList);
            rstMap.remove("success");
        }
        result.setData(rstMap);
        return result;
    }


    @Override
    public void confirmFundPay(String fundPayId, UserAccountVo userAccountVo) {

        List<String> stateList = new ArrayList<String>(){{add( Constant.FundPayState.CREATE); add( Constant.FundPayState.REVOKE);}};
        fundPayReadService.isCanOperate(fundPayId, stateList);


        List<String> ids = fundPayFamilyReadService.getOrderIdsByFundPayId(fundPayId);
        String errorMsg = FundPayBizVerifyUtil.verifyBizIsDone(ids, fundPayFamilyReadService);
        if (Strings.isNotEmpty(errorMsg)) {
            throw new BizException("-1", errorMsg);
        }


        fundPayWriteService.confirmFundPay(fundPayId, userAccountVo);
    }


    @Override
    public String checkCanSyncFundpayData(String fundPayId) {
        FundPayEntity fundPayEntity = fundPayReadService.getById(fundPayId);



        if(!Constant.FundPayState.CONFIRM.equals(fundPayEntity.getStatus())) {
            return "该名册还未确认";
        }
        fundpayExceptionWriteService.checkAndConfirmException(fundPayId, null);

        return null;
    }


    @Override
    public String syncFundpayData(String fundPayId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("fundPayId", fundPayId);
        String retBody = OkHttpUtil.doPost(fundpayUrl, fundPayId);
        if(StringUtils.isNotEmpty(retBody)) {
            retBody = "同步发放名册失败:" + retBody;
        }
        logger.info("同步发放名册结果：{}", retBody);
        return retBody;
    }

    @Override
    public Page<FundpayExceptionEntity> getPageFundpayException(String fundPayId, PageParam pageParam) {
        return fundpayExceptionReadService.getPageExceptionByFundpayId(fundPayId, pageParam);
    }


    @Override
    public void delFundPayObjectList(String fundPayId, List<String> familyIdList) {


        fundPayMemberWriteService.deleteFundPayMember(fundPayId, familyIdList);


        fundPayFamilyWriteService.deleteByFundPayIdAndFamilyId(fundPayId, familyIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }







    @Override
    public void saveCapitalFundPay(CapitalFundPayDTO capitalFundPayDTO, UserAccountVo userAccountVo){

        fundPayReadService.cntCapitalFundPay(capitalFundPayDTO);


        List<String> ids = new ArrayList<>();
        if (Constant.CapitalFundPayCostType.NURSING.equals(capitalFundPayDTO.getCostType())) {
            if (Constant.SupplyType.SUPPLY_DISPERSE.equals(capitalFundPayDTO.getSupplyType())) {

                ids = fundPayFamilyReadService.cntCareOrderByCapital(capitalFundPayDTO, userAccountVo);
            } else {

                List<FundPayFamilyEntity> list = fundPayFamilyReadService.getFocusNurseFamilyByCreateCapital(capitalFundPayDTO);
                ids = list.stream().map(FundPayFamilyEntity::getOrderId).collect(Collectors.toList());
                if(ids.size() <= 0){

                    throw new BizException("-1", "名册为空，无法生成");
                }
            }
        } else {

            ids = fundPayFamilyReadService.cntFamilyByCapitalList(capitalFundPayDTO, userAccountVo);
        }


        String errorMsg = FundPayBizVerifyUtil.verifyBizIsDone(ids, fundPayFamilyReadService);
        if (Strings.isNotEmpty(errorMsg)) {
            throw new BizException("-1", errorMsg);
        }


        fundPayWriteService.insertCapital(capitalFundPayDTO, userAccountVo);

    }


    @Override
    public Page getNoSaveCapitalObjectList(CapitalFundPayDTO capitalFundPayDTO, PageParam pageParam, UserAccountVo userAccountVo){

        FundPayEntity fundPayEntity = fundPayReadService.getById(capitalFundPayDTO.getId());


        this.packageFundPayParam(capitalFundPayDTO, fundPayEntity, userAccountVo);


        return  fundPayFamilyReadService.getNoSaveCapitalFamilyList(capitalFundPayDTO, pageParam);
    }


    private void packageFundPayParam(CapitalFundPayDTO capitalFundPayDTO, FundPayEntity fundPayEntity, UserAccountVo userAccountVo){

        QueryParamUtil.setBaseParamValue(capitalFundPayDTO, userAccountVo);

        capitalFundPayDTO.setStandType(fundPayEntity.getStandType());

        capitalFundPayDTO.setBusinessType(fundPayEntity.getBusinessType());

        capitalFundPayDTO.setPayDate(fundPayEntity.getPayDate());
    }


    @Override
    public void saveCapitalFundPayObject(CapitalFundPayDTO capitalFundPayDTO, List<String> orderIdList) {

        List<String> stateList = new ArrayList<String>(){{add( Constant.FundPayState.CREATE); add( Constant.FundPayState.REVOKE);}};
        fundPayReadService.isCanOperate(capitalFundPayDTO.getId(), stateList);


        fundPayFamilyWriteService.saveCapitalFamilyByOrderId(orderIdList, capitalFundPayDTO);


        fundPayWriteService.updateFundPayNum(capitalFundPayDTO.getId());
    }







    @Override
    public void addStandFundPay(StandFundPayDTO standFundPayDTO, UserAccountVo userAccountVo) {


        fundPayReadService.cntStand(standFundPayDTO);


        List<String> ids = fundPayFamilyReadService.cntFamilyByStand(standFundPayDTO, userAccountVo);


        String errorMsg = FundPayBizVerifyUtil.verifyBizIsDone(ids, fundPayFamilyReadService);
        if (Strings.isNotEmpty(errorMsg)) {
            throw new BizException("-1", errorMsg);
        }

        fundPayWriteService.insertStand(standFundPayDTO, userAccountVo);

    }



    @Override
    public void saveStandObjectList(StandFundPayDTO standFundPayDTO, List<String> orderIdList) {

        List<String> stateList = new ArrayList<String>(){{add( Constant.FundPayState.CREATE); add( Constant.FundPayState.REVOKE);}};
        fundPayReadService.isCanOperate(standFundPayDTO.getId(), stateList);


        fundPayFamilyWriteService.saveDisperseStandFamilyByOrderId(orderIdList, standFundPayDTO);


        fundPayWriteService.updateFundPayNum(standFundPayDTO.getId());
    }


    @Override
    public Page getNoSaveStandObjectList(StandFundPayDTO standFundPayDTO, PageParam pageParam, UserAccountVo userAccountVo){

        FundPayEntity fundPayEntity = fundPayReadService.getById(standFundPayDTO.getId());


        this.packageFundPayParam(standFundPayDTO, fundPayEntity, userAccountVo);


        return  fundPayFamilyReadService.getNoSaveStandFamilyList(standFundPayDTO, pageParam);
    }


    private void packageFundPayParam(StandFundPayDTO standFundPayDTO, FundPayEntity fundPayEntity, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(standFundPayDTO, userAccountVo);

        standFundPayDTO.setStandType(fundPayEntity.getStandType());

        standFundPayDTO.setBusinessType(fundPayEntity.getBusinessType());

        standFundPayDTO.setPayDate(fundPayEntity.getPayDate());

        if(Constant.FundType.STAND_FUND_PAY.equals(standFundPayDTO.getFundType())){

            standFundPayDTO.setSalaryType(
                    Constant.StandFundPayReplacementType.WELFARE.equals(standFundPayDTO.getReplacementType())
                            ? Constant.SalaryType.WELFARE_REISSUE
                            : Constant.SalaryType.SUSPEND_REISSUE);
        }
    }







    @Override
    public void addAllowanceFundPay(AllowanceFundPayDTO allowanceFundPayDTO, UserAccountVo userAccountVo) {

        fundPayReadService.cntAllowance(allowanceFundPayDTO);


        List<String> ids = fundPayFamilyReadService.cntFamilyByAllowance(allowanceFundPayDTO, userAccountVo);


        String errorMsg = FundPayBizVerifyUtil.verifyBizIsDone(ids, fundPayFamilyReadService);
        if (Strings.isNotEmpty(errorMsg)) {
            throw new BizException("-1", errorMsg);
        }


        fundPayWriteService.insertAllowance(allowanceFundPayDTO, userAccountVo);

    }



    @Override
    public void delAllowanceMember(String fundPayId, List<String> memberIdList) {

        fundPayMemberWriteService.delFamilyMemberForByDelMember(fundPayId, memberIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }


    @Override
    public Page getNoSaveAllowanceMemberList(AllowanceFundPayDTO allowanceFundPayDTO, PageParam pageParam, UserAccountVo userAccountVo){

        FundPayEntity fundPayEntity = fundPayReadService.getById(allowanceFundPayDTO.getId());


        this.packageFundPayParam(allowanceFundPayDTO, fundPayEntity, userAccountVo);



        return fundPayMemberReadService.getNoSaveAllowanceFamilyList(allowanceFundPayDTO, pageParam);
    }


    private void packageFundPayParam(AllowanceFundPayDTO allowanceFundPayDTO, FundPayEntity fundPayEntity, UserAccountVo userAccountVo){

        QueryParamUtil.setBaseParamValue(allowanceFundPayDTO, userAccountVo);

        allowanceFundPayDTO.setStandType(fundPayEntity.getStandType());

        allowanceFundPayDTO.setBusinessType(fundPayEntity.getBusinessType());

        allowanceFundPayDTO.setPayDate(fundPayEntity.getPayDate());

        allowanceFundPayDTO.setGrantYears(fundPayEntity.getGrantYears());

        allowanceFundPayDTO.setGrantStandard(fundPayEntity.getGrantStandard());
    }



    @Override
    public void addAllowanceMember(String fundPayId, List<String> orderIdList, List<String> memberIdList) {

        FundPayEntity fundPayEntity = fundPayReadService.getById(fundPayId);


        fundPayMemberWriteService.addMemberForAllowance(fundPayEntity,orderIdList, memberIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }







    @Override
    public void addCustomFundPay(CustomFundPayDTO customFundPayDTO, UserAccountVo userAccountVo) {

        fundPayReadService.cntCustom(customFundPayDTO);


        fundPayWriteService.insertCustom(customFundPayDTO, userAccountVo);
    }


    @Override
    public void updateCustomMemberFundNumber(String fundPayMemberId, String fundPayFamilyId, String fundPayId, double fundNumber){

        fundPayMemberWriteService.updateFundNumber(fundPayMemberId, fundNumber);


        fundPayFamilyWriteService.updateCustomFamilyNumber(fundPayId, fundPayFamilyId);


        fundPayWriteService.updateFundNumber(fundPayId);
    }


    @Override
    public void updateCustomFamilyFundNumber(String fundPayFamilyId, String fundPayId, double fundNumber){


        fundPayFamilyWriteService.updateCustomFamilyNumber(fundPayFamilyId, fundNumber);


        fundPayMemberWriteService.updateMemberFundNumber(fundPayFamilyId, fundNumber);


        fundPayWriteService.updateFundNumber(fundPayId);
    }


    @Override
    public void addCustomMember(String fundPayId, List<String> orderIdList, List<String> memberIdList) {

        FundPayEntity fundPayEntity = fundPayReadService.getById(fundPayId);


        fundPayMemberWriteService.addMemberForCustom(fundPayEntity,orderIdList, memberIdList);


        fundPayWriteService.updateFundFamilyNum(fundPayId);
    }


    @Override
    public void addCustomFamily(String fundPayId, List<String> orderIdList) {

        List<String> stateList = new ArrayList<String>(){{add( Constant.FundPayState.CREATE); add( Constant.FundPayState.REVOKE);}};
        FundPayEntity fundPayEntity = fundPayReadService.isCanOperate(fundPayId, stateList);


        CustomFundPayDTO customFundPayDTO = new CustomFundPayDTO();
        BeanUtils.copyProperties(fundPayEntity, customFundPayDTO);


        fundPayFamilyWriteService.addFamilyForCustom(orderIdList, customFundPayDTO);


        fundPayWriteService.updateFundPayNum(customFundPayDTO.getId());
    }


    @Override
    public void delCustomFamily(String fundPayId, List<String> familyIdList) {

        List<String> stateList = new ArrayList<String>(){{add( Constant.FundPayState.CREATE); add( Constant.FundPayState.REVOKE);}};
        fundPayReadService.isCanOperate(fundPayId, stateList);


        fundPayMemberWriteService.deleteFundPayMember(fundPayId, familyIdList);


        fundPayFamilyWriteService.deleteByFundPayIdAndFamilyId(fundPayId, familyIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }


    @Override
    public void delCustomMember(String fundPayId, List<String> memberIdList) {

        fundPayMemberWriteService.delFamilyMemberForByDelMember(fundPayId, memberIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }


    @Override
    public String verifyConfirmCustom(String fundPayId){

        int rst = fundPayFamilyReadService.cntFundNumberZero(fundPayId);
        if(rst > 0){
            return "存在补发金额为0的信息，请问是否确认";
        }


        rst = fundPayFamilyReadService.verifyNoCompleteNotice(fundPayId);
        if(rst > 0){
            return "当前名册中存在未结束公示的业务，请问是否继续确认";
        }
        return "";
    }


    @Override
    public void confirmCustomFundPay(String fundPayId, UserAccountVo userAccountVo) {

        List<String> stateList = new ArrayList<String>(){{add( Constant.FundPayState.CREATE); add( Constant.FundPayState.REVOKE);}};
        fundPayReadService.isCanOperate(fundPayId, stateList);


        List<String> ids = fundPayFamilyReadService.getOrderIdsByFundPayId(fundPayId);
        String errorMsg = FundPayBizVerifyUtil.verifyBizIsDone(ids, fundPayFamilyReadService);
        if (Strings.isNotEmpty(errorMsg)) {
            throw new BizException("-1", errorMsg);
        }

        fundPayWriteService.confirmFundPay(fundPayId, userAccountVo);




    }







    @Override
    public void addFuneralFundPayOld(FuneralFundPayDTO funeralFundPayDTO, UserAccountVo userAccountVo){

        fundPayReadService.cntFuneral(funeralFundPayDTO);


        fundPayMemberReadService.cntMemberByCreateFuneralOld(funeralFundPayDTO, userAccountVo);


        fundPayWriteService.insertFuneral(funeralFundPayDTO, userAccountVo);

    }





    @Override
    public void addFuneralFundPay(FuneralFundPayDTO funeralFundPayDTO, UserAccountVo userAccountVo){

        fundPayReadService.cntFuneral(funeralFundPayDTO);


        fundPayMemberReadService.cntMemberByCreateFuneral(funeralFundPayDTO, userAccountVo);


        fundPayWriteService.insertFuneral(funeralFundPayDTO, userAccountVo);

    }



    @Override
    public void delFuneralMember(String fundPayId, List<String> memberIdList) {

        fundPayMemberWriteService.delFamilyMemberForByDelMember(fundPayId, memberIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }




    @Override
    public void addFuneralMember(String fundPayId, List<String> orderIdList, List<String> memberIdList) {

        FundPayEntity fundPayEntity = fundPayReadService.getById(fundPayId);


        fundPayMemberWriteService.addMemberForFuneral(fundPayEntity,orderIdList, memberIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }






    @Override
    public Page querySelect(FundPayParam param, PageParam pageParam){
        return  fundPayMemberReadService.querySelect(param,pageParam);
    }




    @Override
    public ExcelInfo exportExcelFundPay(String exportType, List<String> orderIdList, FundPayParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        if(param.getPayDate() != null){
            param.setPayDate(param.getPayDate());
        }
        if("1".equals(exportType)){

            param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,"资金发放名册信息.xls",
                    "资金发放名册列表","资金发放名册列表",false,"payDate","payType","commitDate",
                    "isAuto","payWay","households","standType");
        }else if("2".equals(exportType)){

            param.setFundType(Constant.FundType.STAND_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,"标准补发名册信息.xls",
                    "标准补发名册列表","标准补发名册列表",false,"replacementType","replacementMonth",
                    "remark","payWay","households","standType");
        }else if("3".equals(exportType)){

            param.setFundType(Constant.FundType.ALLOWANCE_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,"补贴发放名册信息.xls",
                    "补贴发放名册列表","补贴发放名册列表",false,"itemType","grantObject","remark",
                    "grantStandard","grantYears","replacementMonth","remark","households","standType");
        }else if("4".equals(exportType)){

            param.setBusinessType(Constant.BusinessCode.TEKUN);
            param.setFundType(Constant.FundType.FUNERAL_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,"丧葬费发放名册信息.xls",
                    "丧葬费发放名册列表","丧葬费发放名册列表",false,"standType","recevierType","payDate",
                    "households","commitDate","status");
        }

        return excelInfo;
    }


    @Override
    public ExcelInfo exportExcelCapitalObjectList(FundPayFamilyParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);



        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);

        List<FundPayFamilyExcelDTO> list  = fundPayFamilyReadService.exportFundPayFamilyList(param);

        excelInfo = exportExcelService.exportOrgOpLogs(FundPayFamilyExcelDTO.class,list,"保障金发放对象信息.xls",
                "保障金发放对象信息","保障金发放对象信息",false,null);


        return excelInfo;
    }


    @Override
    public ExcelInfo exportExcelFundPayMember(String exportType, List<String> orderIdList, FundPayParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        if(param.getPayDate() != null){
            param.setPayDate(param.getPayDate() +"-01");
        }
        String titleName = "";
        if("1".equals(exportType)){

            titleName = "资金发放明细列表";
            param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"资金发放明细信息.xls",
                    titleName,"资金发放明细列表",false,"standType","salvationNo","isHelpPoor","docHelpPoor");
        }else if("2".equals(exportType)){

            titleName = "标准补发明细列表";
            param.setFundType(Constant.FundType.STAND_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"标准补发明细信息.xls",
                    titleName,"标准补发明细列表",false,"standTypet");
        }else if("3".equals(exportType)){

            titleName = "补贴发放明细列表";
            param.setFundType(Constant.FundType.ALLOWANCE_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"补贴发放明细信息.xls",
                    titleName,"补贴发放明细列表",false,"itemType","grantObject","grantStandard");
        }else if("4".equals(exportType)){

            titleName = "自定义发放明细列表";
            param.setFundType(Constant.FundType.CUSTOM_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);

            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"自定义发放明细信息.xls",
                    titleName,"自定义发放明细列表",false,null);
        }

        return excelInfo;
    }


    @Override
    public ExcelInfo downFuneralMemberList(FundPayMemberParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);



        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);

        List<FuneralFundPayExcelDTO> list  = fundPayMemberReadService.exportFuneralMemberList(param);

        excelInfo = exportExcelService.exportOrgOpLogs(FuneralFundPayExcelDTO.class,list,"丧葬费死亡对象明细信息.xls",
                "丧葬费死亡对象明细","丧葬费死亡对象明细",false,null);


        return excelInfo;
    }
    @Override
    public Map<String, String> gainBankInfo(BankAbutmentDTO dto, UserAccountVo userAccountVo) {
        Map<String, String> map = new HashMap<>();
        if(StringUtils.isEmpty(dto.getSsssidcardnum())){
            throw new BizException("-1","身份证号码不能为空");
        }
        if(StringUtils.isEmpty(dto.getSsssobjectname())){
            throw new BizException("-1","姓名不能为空");
        }
        if(StringUtils.isEmpty(dto.getBankcode())){
            throw new BizException("-1","开户银行不能为空");
        }
        if(StringUtils.isEmpty(dto.getAccouttype())){
            throw new BizException("-1","账号类型不能为空");
        }

        if(Constant.FundCapitalType.SOCIAL_SECURITY.equals(dto.getAccouttype())){
            dto.setAccouttype("0");
        }else{
            dto.setAccouttype("1");
        }



        String tenantIdStr = areaReadService.getTenantIdRela(userAccountVo.getOrgId().substring(0,6)+"000000");
        dto.setTenantId(tenantIdStr);

        String bankCode = bankMappingReadService.getFinanceByRescue(dto.getBankcode());
        dto.setBankcode(bankCode);
        dto.setDatasources("01");

        log.info("开始获取财政银行卡信息，参数是：{}", dto);







        String result = fiscalAbutmentService.fiscalAbutmentInfo(dto);

        if(!StringUtils.isEmpty(result)){
            JSONObject jsonObject = JSONObject.parseObject(result);
            String code = jsonObject.getString("code");

            if("-1".equals(code)){
                throw new BizException("-1","当前补贴对象未绑定银行帐号");
            }else{
                String data = jsonObject.getString("data");
                JSONObject jsonData = JSONObject.parseArray(data).getJSONObject(0);
                String accountnum = jsonData.getString("accountnum");
                map.put("accountnum",accountnum);
            }
        }
        return map;
    }


    @Override
    public Integer confirmFundpayException(List<String> exceptionIds) {
        return fundpayExceptionWriteService.confirmException(exceptionIds);
    }


    @Override
    public ExcelInfo exportExcel(String fundpayId) {
        List<FundpayExceptionEntity> list = fundpayExceptionReadService.getExceptionByFundpayId(fundpayId);
        if(null==list || list.size() == 0) {
            return null;
        }
        ExcelInfo excelInfo = exportExcelService.exportOrgOpLogs(FundpayExceptionEntity.class,
                list,"发放名册异常信息表.xls",
                "发放名册异常信息表","名册对象",false,
                "street","community", "name","idcard","saveNumber","salaryNumber","bankName","bankAccount","remark");
        return excelInfo;
    }


    @Override
    public void delAllowanceFundPayFamily(String fundPayId, List<String> familyIdList) {

        fundPayMemberWriteService.delAllowanceRollFamilyAndMemberInfo(fundPayId, familyIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }


    @Override
    public void addAllowanceFundPayFamily(String fundPayId, List<String> orderIdList, List<String> familyIdList) {

        FundPayEntity fundPayEntity = fundPayReadService.getById(fundPayId);


        fundPayMemberWriteService.addAllowanceFundPayFamily(fundPayEntity,orderIdList, familyIdList);


        fundPayWriteService.updateFundPayNum(fundPayId);
    }


    @Override
    public void updateTableData(String changeData, String fundPayId, String familyId, String memberId) {

        fundPayMemberWriteService.updateFundsMember(memberId, changeData);


        fundPayFamilyWriteService.updateFundsFamily(familyId, changeData);


        fundPayWriteService.updateFundNumber(fundPayId);
    }


    @Override
    public void updateRemarkData(String changeData, String fundPayId, String familyId, String memberId) {
        FundPayMemberEntity obj = new FundPayMemberEntity();
        obj.setId(memberId);
        obj.setRemark(changeData);
        fundPayMemberWriteService.updateIgnoreNull(obj);
    }

    @Override
    public List<Map<String,String>> verifyCareStand(UserAccountVo userAccountVo) {
        return fundPayMemberWriteService.verifyCareStand(userAccountVo.getAreaCode());
    }


    @Override
    public ExcelInfo downFuneralTakePersonList(FundPayMemberParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);



        param.setFundType(Constant.FundType.FUNERAL_FUND_PAY);
        List<FuneralFundPayTakePersonExcelDTO> list  = fundPayMemberReadService.exportFuneralTakePersonList(param);

        excelInfo = exportExcelService.exportOrgOpLogs(FuneralFundPayTakePersonExcelDTO.class,list,"丧葬费领取人明细信息.xls",
                "丧葬费发放明细","领取人明细",false,null);


        return excelInfo;
    }

    @Override
    public ExcelInfo exportExcelFundPayFamily(String exportType, List<String> idsList, FundPayParam param, UserAccountVo accountVo) {
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.DIBAO);

        String titleName = "补贴发放明细列表";
        param.setFundType(Constant.FundType.ALLOWANCE_FUND_PAY);
        param.setIdsList(idsList);
        List<FundPayFamilyDetailExcelDTO> list = fundPayMemberReadService.exportExcelFundPayFamily(param);
        excelInfo = exportExcelService.exportOrgOpLogs(FundPayFamilyDetailExcelDTO.class,list,"补贴发放明细信息.xls",
                titleName,"补贴发放明细列表",false,"itemType","grantObject","grantStandard");
        return excelInfo;
    }
}
